#include<stdio.h>


 
 typedef struct ListNode {
     int val;
     struct ListNode *next;
 }LTN;

struct ListNode* removeElements(struct ListNode* head, int val) {
    LTN* newhead, * newtail;
    newhead = newtail = NULL;
    LTN* pcur = head;
    while (pcur)
    {
        if (pcur->val != val)
        {
            if (newhead == NULL)
            {
                newhead = newtail = pcur;
            }
            else {
                newtail->next = pcur;
                newtail = newtail->next;
            }
            
        }
        pcur = pcur->next;
    }
    if (newtail)
        newtail->next = NULL;
    return newhead;
}

